package com.ruoyi.system.service.impl.warehousestate;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.common.CargoStateConstant;
import com.ruoyi.system.common.WarehouseStateConstant;
import com.ruoyi.system.domain.db.trade.TradeCargoOrder;
import com.ruoyi.system.domain.db.transport.TransportTask;
import com.ruoyi.system.domain.dto.TradeCargoOrderDto;
import com.ruoyi.system.domain.dto.TransportTaskDto;
import com.ruoyi.system.domain.dto.WareDto;
import com.ruoyi.system.mapper.basic.BasicWarehousesMapper;
import com.ruoyi.system.mapper.trade.TradeCargoOrderMapper;
import com.ruoyi.system.mapper.transport.TransportTaskMapper;
import com.ruoyi.system.mapper.warehousestate.OrderWarehouseStateMapper;
import com.ruoyi.system.service.OrderWarehouseStateService;
import com.ruoyi.system.utils.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (OrderWarehouseState)表服务实现类
 *
 * @author makejava
 * @since 2023-08-04 16:23:22
 */
@Service
public class OrderWarehouseStateServiceImpl implements OrderWarehouseStateService {
    @Resource
    private OrderWarehouseStateMapper orderWarehouseStateMapper;

    @Resource
    private TransportTaskMapper transportTaskMapper;

    @Resource
    private TradeCargoOrderMapper tradeCargoOrderMapper;

    @Resource
    private BasicWarehousesMapper basicWarehousesMapper;
    /**
     * 运输状态
     *
     * @param taskId
     * @param state
     * @return
     */
    @Override
    @Transactional
    public int changeTaskState(Long taskId, Integer state) {

        // 获取运输任务
        TransportTaskDto transportTaskDto = transportTaskMapper.selectTransportTaskByTaskId(taskId);

        if(state == 2)
        {
            // 运输中
            // 1.更新运输任务状态
            TransportTask transportTask = new TransportTask();
            transportTask.setTaskId(taskId);
            transportTask.setState(CargoStateConstant.IN_TRANSIT);
            transportTaskMapper.updateTransportTask(transportTask);
            // 2.插入订单离开仓库
            List<TradeCargoOrderDto> tradeCargoOrderDtos = tradeCargoOrderMapper.selectCargoInfoByTaskId(taskId);
            orderWarehouseStateMapper.batchInsert(ConvertUtils.tradeCargoOrderDtoList2OrderWarehouseStateList(tradeCargoOrderDtos,WarehouseStateConstant.OUT,taskId,transportTaskDto.getDeparture().getWarehouseId()));
            // 3.更新所有订单运输状态
            tradeCargoOrderMapper.updateTradeCargoOrderStateByTaskId(taskId,CargoStateConstant.IN_TRANSIT, DateUtils.getNowDate());
        }else{
            // 已完成
            // 1.更新运输任务状态
            TransportTask transportTask = new TransportTask();
            transportTask.setTaskId(taskId);
            transportTask.setState(CargoStateConstant.FINISHED);
            transportTaskMapper.updateTransportTask(transportTask);
            // 2.插入订单到达仓库
            List<TradeCargoOrderDto> tradeCargoOrderDtos = tradeCargoOrderMapper.selectCargoInfoByTaskId(taskId);
            orderWarehouseStateMapper.batchInsert(ConvertUtils.tradeCargoOrderDtoList2OrderWarehouseStateList(tradeCargoOrderDtos,WarehouseStateConstant.IN,taskId,transportTaskDto.getDestination().getWarehouseId()));
            // 3.更新所有订单状态
            // 3.1 先把所有订单都设置为已揽收
            tradeCargoOrderMapper.updateTradeCargoOrderStateByTaskId(taskId,CargoStateConstant.WAITING, DateUtils.getNowDate());
            // 3.2 若有订单已经到达所在仓库，则更新订单状态已送达
            List<WareDto> wareDtos = basicWarehousesMapper.selectWareDtoList(null);
            // 检查某些订单已经到达订单所要去的仓库
            checkArrive(wareDtos,tradeCargoOrderDtos,taskId);
        }
        return 1;
    }

    /**
     * 列出该司机获取运输任务所有
     *
     * @param driverId
     */
    @Override
    public List<TransportTaskDto> listTransportTaskByDriverId(Long driverId) {
        List<TransportTaskDto> transportTaskDtos = transportTaskMapper.selectTransportTaskListByDriverId(driverId);
        return transportTaskDtos;
    }

    /**
     * 获取运输任务
     *
     * @param taskId
     */
    @Override
    public TransportTaskDto getTransportTask(Long taskId) {
        return transportTaskMapper.selectTransportTaskByTaskId(taskId);
    }


    /**
     * 检查是否已经到达订单所要去的仓库
     */
    public void checkArrive(List<WareDto> wareDtos,List<TradeCargoOrderDto> orderDtos,Long taskId)
    {
       for(TradeCargoOrderDto orderDto :orderDtos)
       {
              for(WareDto wareDto:wareDtos)
              {
                if(orderDto.getDestinationDistrict().equals(wareDto.getDistrict())&&orderDto.getDestinationTown().equals(wareDto.getTown()))
                {
                     // 到达
                    TradeCargoOrder tradeCargoOrder = new TradeCargoOrder();
                    tradeCargoOrder.setOrderId(orderDto.getOrderId());
                    tradeCargoOrder.setState(CargoStateConstant.FINISHED);
                    tradeCargoOrder.setUpdateTime(DateUtils.getNowDate());
                    tradeCargoOrder.setUpdateBy(SecurityUtils.getUserId());
                    tradeCargoOrderMapper.updateTradeCargoOrder(tradeCargoOrder);
                    // 出库
                    orderWarehouseStateMapper.insert(ConvertUtils.tradeCargoOrderDto2OrderWarehouseState(orderDto, WarehouseStateConstant.OUT,taskId,wareDto.getWarehouseId(),wareDto.getManager().getUserId()));

                }
              }
       }
    }




}
